using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna;

namespace CastleDefender.Platform.TouchBehavior
{
    public class PinchZoom
    {
        public static float GetScaleFactor(Vector2 position1, Vector2 position2, 
            Vector2 oldPosition1, Vector2 oldPosition2)
        {
            return Vector2.Distance(position1, position2)
                / Vector2.Distance(oldPosition1, oldPosition2);
        }

        public static Vector2 GetTranslationDelta(Vector2 position1, Vector2 position2,
            Vector2 oldPosition1, Vector2 oldPosition2, Vector2 posObject, float scaleFactor)
        {
            var newpos1 = position1 + (posObject - oldPosition1) * scaleFactor;
            var newpos2 = position2 + (posObject - oldPosition2) * scaleFactor;

            var newpos = (newpos1 + newpos2) / 2;

            return newpos - posObject;
        }

        public static void ApplyPinchZoom(Vector2 position1, Vector2 position2, 
            Vector2 oldPosition1, Vector2 oldPosition2, ref Vector2 objectPos, ref float objectScale)
        {
            var scaleFactor = GetScaleFactor(position1, position2, oldPosition1, oldPosition2);
            
            objectScale *= scaleFactor;
            objectPos += GetTranslationDelta(position1, position2, oldPosition1, oldPosition2, objectPos, scaleFactor);
        }
    }
}
